package org.hishenframework.core.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

public class UserFilterInvocationSecurityMetadataSource implements
		FilterInvocationSecurityMetadataSource {
	// 资源权限对应map
	private Map<String, List<ConfigAttribute>> roleToResource;

	private PathMatcher pathMatcher;

	private List<Map<String, Object>> resources, rolesByReource;
	private Iterator<Map<String, Object>> iteratorOfResources,
			iteratorOfRolesByReource;

	@Resource
	private SecurityService securityService;

	
	public UserFilterInvocationSecurityMetadataSource() {
		super();
//		this.roleToResource = new HashMap<String, List<ConfigAttribute>>();
//		List<ConfigAttribute> list = new ArrayList<ConfigAttribute>();
//		list.add(new SecurityConfig("ROLE_ANONYMOUS"));
//		this.roleToResource.put("/admin/login/**", list);
	}

	@Override
	public Collection<ConfigAttribute> getAllConfigAttributes() {
		return null;
	}

	@Override
	public Collection<ConfigAttribute> getAttributes(Object arg0)
			throws IllegalArgumentException {
		String requestUrl = ((FilterInvocation) arg0).getRequestUrl();
		Iterator<String> iter = this.roleToResource.keySet().iterator();
		while (iter.hasNext()) {
			String temp = iter.next();
			if (this.pathMatcher.match(temp, requestUrl)) {
				return roleToResource.get(temp);
			}
		}
		return null;
	}
	
	@PostConstruct
	public void initDate(){
		List<ConfigAttribute> tempList;
		Map<String, Object> tempResource, tempRoleByResource;
		this.roleToResource = new HashMap<String, List<ConfigAttribute>>();
		this.pathMatcher = new AntPathMatcher();
		resources = securityService.getAllResources();
		rolesByReource = securityService.getRolesByResource();
		iteratorOfResources = resources.iterator();
		while (iteratorOfResources.hasNext()) {
			tempList = new ArrayList<>();
			iteratorOfRolesByReource = rolesByReource.iterator();
			tempResource = iteratorOfResources.next();
			while (iteratorOfRolesByReource.hasNext()) {
				tempRoleByResource = iteratorOfRolesByReource.next();
				if (tempRoleByResource.get("resource_id") == tempResource
						.get("id")) {
					tempList.add(new SecurityConfig((String) tempRoleByResource
							.get("role")));
				}
			}
			roleToResource.put((String) tempResource.get("resource"),
					tempList);
		}
	}

	@Override
	public boolean supports(Class<?> arg0) {
		return true;
	}

	public Map<String, List<ConfigAttribute>> getRoleToResource() {
		return roleToResource;
	}

}
